I'm going to plot the shot chart for every MVP winner since the 96-97 season (data is not available earlier than that):
In [12]:
import pandas as pd
import NBAapi as nba
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import urllib, cStringIO
from scipy import misc
import seaborn
%matplotlib inline
In [13]:
def seasons_string(start,end):
'''
creates a list of NBA seasons from start-end
'''
years = np.arange(start,end+1)
seasons = []
for year in years:
string1 = str(year)
string2 = str(year+1)
season = '{}-{}'.format(string1,string2[-2:])
seasons.append(season)
return seasons
Let's load the list of players from the NBA API:
In [3]:
player_list = nba.player.commonallplayers(currentseason=0) # load a list of NBA players including retired players
And now let's create a list of names and a list of seasons:
In [15]:
players = ['Malone, Karl',
'Jordan, Michael',
'Malone, Karl',
'O\'Neal, Shaquille',
'Iverson, Allen',
'Duncan, Tim',
'Duncan, Tim',
'Garnett, Kevin',
'Nash, Steve',
'Nash, Steve',
'Nowitzki, Dirk',
'Bryant, Kobe',
'James, LeBron',
'James, LeBron',
'Rose, Derrick',
'James, LeBron',
'James, LeBron',
'Durant, Kevin',
'Curry, Stephen',
'Curry, Stephen']
# create season string based on the seasons Steph Curry played in the league
seasons = seasons_string(1996,2016)
for season,player in zip(seasons,players):
print(season + ': ' + player)
And now we can easily create the shot charts following the guidelines from this post:
In [11]:
for season,player in zip(seasons,players):
player_id = player_list[player_list['DISPLAY_LAST_COMMA_FIRST']== player].PERSON_ID # get players id
shotchart,leagueavergae = nba.shotchart.shotchartdetail(playerid=player_id,season=season) # get shot chart data from NBA.stats
nba.plot.grantland_shotchart(shotchart,leagueavergae)
plt.text(0,38,season,fontsize=20,horizontalalignment='center',verticalalignment='center')
Comments
comments powered by Disqus